<template>
  <div class="box-all w-card">
    <div class="content-box">
      <div class="title-box">
        <span>库存列表</span>
        <el-button style="margin-left:auto;" @click="handleClick(2)">批量退库</el-button>
        <el-button type="primary" @click="handleClick(1)">批量入库</el-button>
      </div>
      <div class="search-box">
        <!-- <div>
          <span>类型：</span>
          <el-select style="width:100%" v-model="searchForm.typeId" clearable @change="typeChange">
            <el-option v-for="item in typeOptions" :key="item.typeId" :label="item.typeName" :value="item.typeId" />
          </el-select>
        </div> -->
        <div>
          <span>分类：</span>
          <el-select style="width:100%" v-model="searchForm.categoryId" clearable no-data-text="请先选择类型" @change="cateChange">
            <el-option v-for="item in simpleOptions" :key="item.categoryId" :label="item.categoryName" :value="item.categoryId" />
          </el-select>
        </div>
        <div>
          <span>品牌：</span>
          <el-select style="width:100%" v-model="searchForm.brandId" clearable no-data-text="请先选择分类" @change="brandChange">
            <el-option v-for="item in brandOptions" :key="item.brandId" :label="item.brandName" :value="item.brandId" />
          </el-select>
        </div>
        <div>
          <span>商品：</span>
          <el-select style="width:100%" v-model="searchForm.goodsId" clearable no-data-text="请先选择品牌" @change="goodsChange">
            <el-option v-for="item in goodsOptions" :key="item.goodsId" :label="item.goodsName" :value="item.goodsId" />
          </el-select>
        </div>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="goodsName" label="商品名称" align="center">
            <template v-slot="scope">
              <el-link type="primary" @click="lookDetail(scope.row)">{{ scope.row.goodsName || '' }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="shopName" label="品类" align="center" min-width="200">
            <template v-slot="scope">
              <span>{{ scope.row.brandName }}</span>
              <span>【{{ scope.row.categoryName }}】</span>
            </template>
          </el-table-column>
          <el-table-column prop="toSaleNum" label="可售" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.toSaleNum) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="saleIngNum" label="销售中" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.saleIngNum) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="soldNum" label="售出" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.soldNum) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="exchangeNum" label="兑换" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.exchangeNum) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="cancelNum" label="作废" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.cancelNum) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="expireNum" label="过期" align="center" min-width="100">
            <template v-slot="scope">
              {{ numFormat(scope.row.expireNum) || '' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <in-out-dialog ref="inOutRef" @dialogClose="inOutClose" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import api from './api'
import { getWindowWidthAndHeight, warningDeal, numFormat } from '@/utils/utils'
import inOutDialog from './components/inOutDialog.vue'
import { useRouter } from 'vue-router'
export default {
  components: {
    inOutDialog,
  },
  setup() {
    const router = useRouter()

    const inOutRef = ref(null)

    const state = reactive({
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      tableData: [],
      searchForm: {
        typeId: '3'
      },
      typeOptions: [],
      simpleOptions: [],
      brandOptions: [],
      goodsOptions: [],
    })

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function typeChange(e) {
      return
      state.searchForm.categoryId = null
      state.searchForm.brandId = null
      state.simpleOptions = []
      state.brandOptions = []
      state.searchForm.goodsId = null
      state.goodsOptions = []
      searchHandle()
      if (!e) {
        state.simpleOptions = []
        state.brandOptions = []
        return
      }
      getSimOptions(e)
    }

    function cateChange(e) {
      state.searchForm.brandId = null
      state.brandOptions = []
      state.searchForm.goodsId = null
      state.goodsOptions = []
      searchHandle()
      if (!e) {
        state.brandOptions = []
        return
      }
      goodsBrandSimple(e)
    }

    function brandChange(e) {
      state.searchForm.goodsId = null
      state.goodsOptions = []
      searchHandle()
      if (!e) {
        return
      }
      getGoodsOptions(e)
    }

    function goodsChange() {
      searchHandle()
    }

    function getSimOptions(typeId) {
      const obj = {
        typeId
      }
      api.goodsCategoryListSimple(obj).then(res => {
        if (res.code == '200') {
          state.simpleOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getTypeOptions() {
      api.goodsTypeListSimple().then(res => {
        if (res.code == '200') {
          state.typeOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function goodsBrandSimple(categoryId) {
      const obj = {
        categoryId
      }
      api.goodsBrandSimple(obj).then(res => {
        if (res.code == '200') {
          state.brandOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getGoodsOptions(brandId) {
      const obj = {
        brandId
      }
      api.goodsInfoSimple(obj).then(res => {
        if (res.code == '200') {
          state.goodsOptions = res.data
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
      }
      api.cardInventorySearch(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.records
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleClick(i) {
      inOutRef.value.dialogShow(i)
    }

    function inOutClose() {
      getTableData()
    }

    function lookDetail(data) {
      router.push({ path: '/inventory/detail', query: { goodsId: data.goodsId, goodsName: data.goodsName } })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      getTableData()
      //   getTypeOptions()
      getSimOptions('3')
    })

    return {
      ...toRefs(state),
      inOutRef,
      typeChange,
      cateChange,
      brandChange,
      numFormat,
      goodsChange,
      handleClick,
      inOutClose,
      lookDetail,
      handleCurrentChange,
      handleSizeChange,
    }
  }
}
</script>

<style lang="scss" scoped>
.box-all {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      > span {
        font-size: 16px;
        font-weight: bold;
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      gap: 10px;
      margin-top: 20px;
      > div {
        display: flex;
        align-items: center;
        font-size: 14px;
        > span {
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        display: flex;
        align-items: center;
        margin-top: 10px;
      }
    }
  }
}
</style>